//
//  HMDocDetailController.m
//  仿真开发
//
//  Created by LZH on 2017/3/23.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMDocDetailController.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "HMbounceTool.h"

@interface HMDocDetailController () <UIScrollViewDelegate>

@property(strong,nonatomic)NSMutableArray<UIView *> *viewArrs;
@property(weak,nonatomic)UILabel *require;
@property(weak,nonatomic)UILabel *introduction;
@property(assign,nonatomic)BOOL isSelect;

//头像
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
//姓名
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//职位
@property (weak, nonatomic) IBOutlet UILabel *postion;
//收藏
@property (weak, nonatomic) IBOutlet UIButton *collect;
//医院名称
@property (weak, nonatomic) IBOutlet UILabel *hospitalName;
@property (weak, nonatomic) IBOutlet UIView *selectView;
@property (weak, nonatomic) IBOutlet UILabel *selectRightView;
@property (weak, nonatomic) IBOutlet UILabel *selectLeftView;



@property (weak, nonatomic) IBOutlet UIScrollView *ScrollView;

// 滚动指示器
 @property(strong,nonatomic)UIView *indicator;

//医生详情模型数组

@property(strong,nonatomic)NSArray<HMDocDetailModel *> *details;

//医生简介模型数组

@property(strong,nonatomic)NSArray<HMDocIntroduceModel *> *intros;

// 医生接诊条件模型数组

@property(strong,nonatomic)NSArray<HMDocRequireModel *> *requires;
@end

@implementation HMDocDetailController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"医生详情";
    [self setupUI];
    _details=[NSArray array];
    _intros=[NSArray array];
    _requires=[NSArray array];

    
    
    
    //查看医生接诊条件
    //NSString *url = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    //NSDictionary *parameters = @{@"doctor_id":@300000315};
    //获取医生接诊条件数据
   
    
    NSString *introURL = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    NSDictionary *introsParameter = @{@"doctor_id":@300000315};

    [HMDocIntroduceModel getIntrodDataWithURLString:introURL andParameters:introsParameter andSuccessBlock:^(NSArray *newsList) {
        
        self.requires = newsList;
       HMDocIntroduceModel *model =  newsList[0];
        self.introduction.text = model.introduction;
        
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    
    
    
    // 医生简介接口
    //NSString *url = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    //NSDictionary *parameters = @{@"doctor_id":@300000315};
    // 获取医生简介数据
    NSString *conditionURL = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    
    NSDictionary *conditionParameter = @{@"doctor_id":@300000315};
    [HMDocRequireModel getDocRequireDataWithURLString:conditionURL andParameters:conditionParameter  andSuccessBlock:^(NSArray *newsList) {
        
        self.intros = newsList;
        
        HMDocRequireModel *model =  newsList[0];
        self.require.text = model.receiving_setting_extra;
        
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
}




-(void)setupUI{
    
    self.viewArrs = [NSMutableArray arrayWithCapacity:2];
    
    
    //导航栏右上角的收藏键
      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"saveTemplateEmpty"] style:UIBarButtonItemStylePlain target:self action:@selector(clickNavRightBarBtn:)];
    
    //设置label
   // self.selectLeftView.textColor =[UIColor redColor];
    self.selectLeftView.tag = 1;
    self.selectLeftView.userInteractionEnabled = YES;
    
    //self.selectRightView.textColor =[UIColor yellowColor];
    self.selectRightView.tag = 2;
    self.selectRightView.userInteractionEnabled = YES;
    
    //添加手势
    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.selectLeftView addGestureRecognizer:tapLeft];
    
    UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.selectRightView addGestureRecognizer:tapRight];
    
    
    //滚动条
    UIView *srcollBar = [[UIView alloc]init];
    srcollBar.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:srcollBar];
    
    [srcollBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.selectLeftView.mas_bottom);
        make.height.mas_equalTo(3);
        make.width.equalTo(self.selectLeftView);
    }];
    
    self.indicator = srcollBar ;
    
    
    // 设置头像图片成圆图
    self.iconImage.layer.masksToBounds = YES;
    self.iconImage.layer.cornerRadius = self.iconImage.bounds.size.width*0.5;
    self.iconImage.layer.borderWidth = 3;
    self.iconImage.layer.borderColor = [UIColor whiteColor].CGColor;
    
    // 设置scrollview相关属性
    _ScrollView.showsHorizontalScrollIndicator=YES;
    _ScrollView.pagingEnabled=YES;
    
    [self.ScrollView setBackgroundColor:[UIColor whiteColor]];
    
    [self setBottomUI];
    
    self.ScrollView.delegate = self;
}

//MARK:添加底部视图到srollView
-(void)setBottomUI{
    
    //定义一个宽
    CGFloat width = self.view.bounds.size.width;
    //定义一个高
    CGFloat height = self.ScrollView.bounds.size.height;
   
    //定义个数
    int index = 2;
    
    for (int i = 0; i < index; i++) {
        
        
        UIView *bView = [[UIView alloc]init];
        
        //添加到ScrollView上
        [self.ScrollView addSubview:bView];
       
        [self.viewArrs addObject:bView];
        
        bView.frame = CGRectMake(width*i, 0, width, height);
    }
    
    //设置偏移量
    self.ScrollView.contentSize = CGSizeMake(width*index, 0);
    
    
    
    //设置接诊条件和医生简介内容
  [self setBottomContent];
}



//MARK:设置底部文字显示
-(void)setBottomContent{
    
    //接诊条件文字
    UILabel *requirText = [[UILabel alloc]init];
    
    requirText.text=@"有钱吗?";
    [self.viewArrs[0] addSubview:requirText];
    requirText.textAlignment = NSTextAlignmentLeft;

    [requirText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.viewArrs[0]).mas_offset(15);
        make.right.equalTo(self.viewArrs[0]).mas_offset(-15);
    }];

    requirText.lineBreakMode=NSLineBreakByWordWrapping;
    requirText.numberOfLines = 0;
    self.require = requirText;
    
    
    //医生简介
    UILabel *intro = [[UILabel alloc]init];
     intro.text=@"包你满意";
    [self.viewArrs[1] addSubview:intro];
   
    
    
   intro.textAlignment = NSTextAlignmentLeft;
    [intro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.viewArrs[1]).mas_offset(15);
        make.right.equalTo(self.viewArrs[1]).mas_offset(-15);
    }];

    intro.lineBreakMode=NSLineBreakByWordWrapping;
    intro.numberOfLines = 0;
    self.introduction = intro;
}


//MARK:点击label触发
-(void)clickLabel:(UITapGestureRecognizer *)sender{
    
    if (sender.view.tag == 1) {
        
        [self.ScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
        // 开始动画
        [UIView beginAnimations:nil context:nil];
        // 动画时长
        [UIView setAnimationDuration:1.0];
        
        [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view);
            make.top.equalTo(self.selectLeftView.mas_bottom);
        }];
        // 提交动画
        [UIView commitAnimations];
    }
    else if(sender.view.tag == 2){
        
        [self.ScrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
        
        [UIView animateWithDuration:0.6 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/2);
                make.top.equalTo(self.selectRightView.mas_bottom);
            }];
        }];
    }
}


//MARK:scrollview滚动停止后执行
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat width = self.view.bounds.size.width;
    NSInteger index = self.ScrollView.contentOffset.x/width;
   
    if (index == 0) {
       
        
        [UIView animateWithDuration:1 animations:^{
           [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
              make.left.equalTo(self.view);
              make.top.equalTo(self.selectLeftView.mas_bottom);
           }];
        }];
        
    }else if (index == 1){
       
        [UIView animateWithDuration:1 animations:^{
          
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
              make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/2);
              //make.right.equalTo(self.view.mas_right);
              make.top.equalTo(self.selectRightView.mas_bottom);
           }];
        }];
    }
}




//MARK:setter方法调用
-(void)setDocListModel:(HMDoctorListModel *)docListModel{

    _docListModel = docListModel;


}

-(void)setDocRequireMode:(HMDocRequireModel *)docRequireMode{
    
    NSMutableString *textStr = [NSMutableString string];
    
    for (NSString *s in docRequireMode.receiving_settings) {
        [textStr appendFormat:@"\n\t%@\n",s];
    }
    self.require.text = [NSString stringWithFormat:@"\t%@%@",docRequireMode.receiving_setting_extra,textStr];
}

-(void)setIntroduceModel:(HMDocIntroduceModel *)introduceModel{
    self.introduction.text=introduceModel.introduction;

}

//
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.nameLabel.text = _docListModel.doctor_name;
    self.postion.text = _docListModel.doctor_title_name;
    
    self.hospitalName.text = _docListModel.hospital_name;
    
    
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:_docListModel.doctor_portrait] placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
}






//MARK:收藏键的点击事件
-(void)clickNavRightBarBtn:(UIBarButtonItem *)sender{
    if (!self.isSelect) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplate"]];

        [HMbounceTool CKLShowSVSuccessTitle:@"成功关注" andTime:1];
    }else{
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplateEmpty"]];

        [HMbounceTool CKLShowSVSuccessTitle:@"取消关注" andTime:1];
    }
    self.isSelect = ! self.isSelect;
}









@end










